<?php

namespace app\api\controller;

use app\admin\model\store\Servec;
use app\admin\model\technician\CheckUser;
use app\admin\model\time\Quantum;
use app\admin\model\Updateintro;
use app\common\controller\Api;
use app\common\model\CommentZan;
use app\common\model\EnterPriseCheck;
use app\common\model\Follow;
use app\common\model\InformLog;
use app\common\model\OrderComment;
use app\common\model\SearchLog;
use app\common\model\ServeLabel;
use app\common\model\StoreExamine;
use app\common\model\Technician;
use app\common\model\TechnicianServe;
use think\Db;
use think\Loader;

class Store  extends Api
{
    protected $noNeedLogin = ['updateIntroList','updateIntrodetail'];
    protected $noNeedRight = ['*'];


    /**
     * 店铺添加关注/取消关注
     *
     */
    public function follow_store(){
        $uid           = $this->auth->id;
        $type          = $this->request->request('type');//1关注 2取消关注
        $store_id=$this->request->request('store_id');
        $postion=$this->auth->postion;
        if($postion!=1){
            $this->error('当前身份不是用户');
        }
        if($type==1){
$store_info=\app\common\model\Store::get($store_id);
            $old_data        = Follow::where(['uid' => $uid, 'store_id' => $store_id])->find();
            if($old_data){
                if($old_data['follow_status'] == 1){
                    $this->error('您已关注当前店铺');
                }else{
                    $res = $old_data->save(['follow_status' => 1]);
                }
            }else{
                $data = [
                    'uid'               => $uid,
                    'store_id'     => $store_id,
                    'store_name' => $store_info->name,
                    'follow_status'     => 1,
                     'follow_time'=>time(),
                    'createtime'        => time(),
                    'type'=>3
                ];
                $res  = Follow::create($data);
            }
            \app\common\model\User::where('id', $uid)->setInc('store_follow');
        }else if($type==2){
$store_info=Follow::where(['uid'=>$uid,'store_id'=>$store_id,'follow_status'=>1])->find();
if(!$store_info){
    $this->error('你未关注当前店铺');
}
$res=$store_info->save(['follow_status' => 2]);
            \app\common\model\User::where('id', $uid)->setDec('store_follow');
        }
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 店铺介绍
     * store_id
     */

    public function storeIntrouce(){

        $uid=$this->auth->id;
        $store_id=$this->request->request('store_id');

        if(!$store_id){
            $this->error('参数错误');
        }
        $type=$this->request->request('type');
        $page=$this->request->request('page',1);
        $where=[];
        if($type){
            $where['type']=$type;
        }
        $comment=OrderComment::where($where)->page($page,10)->field('id,uid,address,technician_id,star,reply_content,serve_id,createtime,image,content')->select();
        $store=\app\common\model\Store::where('store_id','=',$store_id)->field('store_id,name,is_check,lincense,feedback,order_rate,sales,company_name,store_introduction,image,store_grade')->find();
        if($store){
            $store['lincense']=explode(',',$store['lincense']);
          if(!$store['company_name']){
              $store['company_name']=$store['name'];
          }
          if($store['store_grade']){
              $store['store_grade']=intval($store['store_grade']);
          }
            $follow_status=Follow::where(['uid'=>$uid,'store_id'=>$store_id,'follow_status'=>1])->find();
            if($follow_status){
                $store['follow_status']='已关注';
            }else{
                $store['follow_status']='未关注';
            }
            $store['haoping']=OrderComment::where(['star'=>5,'store_id'=>$store_id])->count();
            $store['zhongping']=OrderComment::where(['star'=>['in',[2,3,4]],'store_id'=>$store_id])->count();
            $store['chaping']=OrderComment::where(['star'=>1,'store_id'=>$store_id])->count();
            $store['quanbu']=OrderComment::where(['store_id'=>$store_id])->count();
            $store['youtu']=OrderComment::where(['store_id'=>$store_id,'type'=>4])->count();
            $store['comment']=$comment;
           foreach ($store['comment'] as $item){
            $store['comment_image']=explode(',',$item['image']);
           }
        }
        $this->success('查询成功',$store);

    }


    /**
     * 店铺技师
     */

    public function store_technician(){
      $store_id = $this->request->request('store_id');//店铺ID
      $store_info = \app\common\model\Store::where('store_id',$store_id)->field('store_id,name,image,lincense,company_name,store_grade')->find();
      $uid=$this->auth->id;
      $follow=Follow::where(['uid'=>$uid,'store_id'=>$store_id,'follow_status'=>1])->find();
      if($follow){
        $info='已关注';
      }else{
        $info='未关注';
      }
      $store_info['follow']=$info;
      $store_info['technician_list'] = \app\common\model\Technician::where(['store_id'=>$store_id,'is_join_store'=>1])->field('technician_id,technician_name,technician_address,technician_age,technician_gender,technician_avatar,real_name,introduction,is_check,serve_time,serve_time_end,feedback,technician_fraction')->select();
      foreach ($store_info['technician_list'] as $key=>$v){
          $v['technician_address']=mb_substr($v['technician_address'],0,2);
          $v['technician_fraction']=intval( $v['technician_fraction']);
        $isset_order = \app\common\model\Order::where(['technician_id' => $v['technician_id'],'order_status' => ['IN', [2, 3, 4, 5]]])->find();
        if($isset_order){
          $v['earliestappionent_time'] = date('H:i', $isset_order['endserve_time']);
        }else{
          $v['earliestappionent_time'] =date('H:i',time());
        }
      }
      $this->success('',$store_info);
    }

    /**
     * 店铺服务
     */

    public function store_serve(){
      $page=$this->request->request('page','1');
      $store_id = $this->request->request('store_id');//店铺ID
      $price=$this->request->request('price');
      $order=$this->request->request('order');
      $uid=$this->auth->id;
      if(!$store_id){
        $this->error('参数不能为空');
      }
      $store_info = \app\common\model\Store::where('store_id',$store_id)->field('name,image,company_name,lincense,store_id,store_grade')->find();
      $tserve=TechnicianServe::where(['store_id'=>$store_id,'status'=>1])->group('serve_id')->column('serve_id');
      if($store_info){
        $follow_status=Follow::where(['uid'=>$uid,'store_id'=>$store_id])->field('follow_status')->find();
        if($follow_status){
          if($follow_status->follow_status=1){
            $store_info['follow_status']='已关注';
          }else{
            $store_info['follow_status']='未关注';
          }
        }
      }
      $where=[];
      $where['serve_status']=1;
      $where['is_added']=0;
      if ($store_id) {
        $where['store_id'] = $store_id;
      }
      if($tserve){
        $where['id']=['in',$tserve];
      }
      if ($order) {
        if ($order == 1) {
          $order = 'sales desc';
        } else {
          $order = 'sales asc';
        }
      }
      if($price){
        if ($price == 1) {
          $order = 'serve_price desc';
        } else {
          $order = 'serve_price asc';
        }
      }
       $serve=\app\common\model\Serve::where($where)->field('id,serve_name,serve_image,serve_duration,serve_price,sales')->order($order)->page($page)->select();
       $store_info['serve']=$serve;
      $this->success('',$store_info);
    }

    /**
     * 店铺评论
     */

    public function storeComment(){
        $uid=$this->auth->id;
        $store_id=$this->request->request('store_id');
        $type=$this->request->request('type');
        $page=$this->request->request('page');
        $store_info = \app\common\model\Store::where('store_id',$store_id)->field('name,image,company_name,lincense,store_grade,store_id')->find();
        if($store_info){
            $follow_status=Follow::where(['uid'=>$uid,'store_id'=>$store_id])->field('follow_status')->find();
            if($follow_status){
                if($follow_status->follow_status=1){
                    $store_info['follow_status']='已关注';
                }else{
                    $store_info['follow_status']='未关注';
                }
            }
        }

        $where=[];
        if($type){
            $where['type']=$type;
        }
        $comment=OrderComment::where($where)->page($page,10)->field('id,uid,address,technician_id,star,reply_content,serve_id,createtime,image,content')->select();
        $haoping=OrderComment::where(['star'=>5,'store_id'=>$store_id])->count();
        $zhongping=OrderComment::where(['star'=>['in',[2,3,4]],'store_id'=>$store_id])->count();
        $chaping=OrderComment::where(['star'=>1,'store_id'=>$store_id])->count();
        $quanbu=OrderComment::where(['store_id'=>$store_id])->count();
        $youtu=OrderComment::where(['store_id'=>$store_id,'type'=>4])->count();
        foreach ($comment as $item){
            $item['image']=explode(',',$item['image']);
            $item['haoping']=$haoping;
            $item['zhongping']=$zhongping;
            $item['chaping']=$chaping;
            $item['quanbu']=$quanbu;
            $item['youtu']=$youtu;
            $item['createtime']=date('Y-m-d H:i',$item['createtime']);
            $item['user']=\app\common\model\User::where(['id'=>$item['uid'],'del_flag'=>0])->field('id,avatar,nickname')->find();
            $item['technician']= \app\common\model\Technician::where('technician_id', '=', $item['technician_id'])->field('technician_id,store_id,technician_avatar,real_name')->find();
            $item['serve'] = \app\common\model\Serve::where('id', '=', $item['serve_id'])->field('id,serve_name')->find();
            $zan=CommentZan::where(['uid'=>$uid,'zan'=>1])->find();
            if(!$zan) {
                $item['zan']='未点赞';
            }else{
                if($zan['zan']==1){
                    $item['zan']='已点赞';
                }
            }
            $item['zancount']=CommentZan::where(['comment_id'=>$item['id'],'zan'=>1])->count();
        }
$this->success('查询成功',$comment);
    }

    /**
     * 店铺首页
     */

    public function store_index(){
        $uid=$this->auth->id;
        $store_id=$this->request->request('store_id');
        $store_info = \app\common\model\Store::where('store_id',$store_id)->field('name,image,company_name,lincense,store_grade,store_id')->find();
        if($store_info){
            $follow_status=Follow::where(['uid'=>$uid,'store_id'=>$store_id])->field('follow_status')->find();
            if($follow_status){
                if($follow_status->follow_status=1){
                    $store_info['follow_status']='已关注';
                }else{
                    $store_info['follow_status']='未关注';
                }
            }
        }
        $list=[];
        $technician=\app\common\model\Technician::where(['store_id'=>$store_id,'recommend_status'=>1])->field('technician_id,technician_avatar,technician_name')->select();
        $list['technician_count']=\app\common\model\Technician::where(['store_id'=>$store_id,'recommend_status'=>1])->count();
        $serveids=TechnicianServe::where(['store_id'=>$store_id])->group('serve_id')->column('serve_id');
        $serve=\app\common\model\Serve::where(['id'=>['in',$serveids],'is_recommend'=>1])->field('serve_name,id,serve_image,serve_price,serve_duration,sales')->select();
        $list['technician']=$technician??[];
        $list['serve']=$serve??[];
        $this->success('查询成功',$list);
    }


    /**
     * 查询当前店铺的服务
     */


    public function storeserve(){
        $uid=$this->auth->id;
        $technician=\app\common\model\Technician::where('uid','=',$uid)->group('store_id')->column('store_id');
        $tserve=TechnicianServe::where(['store_id'=>['in',$technician]])->group('serve_cid')->column('serve_cid');
        $servc=Servec::where(['id'=>['in',$tserve]])->select();
        $servc['count']=\app\common\model\Serve::where(['serve_cid'=>['in',$tserve],'serve_status'=>1,'is_added'=>0])->count();//服务分类下面的服务数量
        $servc['addcount']=TechnicianServe::where(['serve_cid'=>['in',$tserve],'technician_id'=>['in',$uid]])->count();
        $this->success('查询成功',$servc);
    }


    /**
     * 快速开店
     */


    public function  QuicklyShop(){
      $uid=$this->auth->id;
      $postion=$this->auth->postion;
      $mobile=$this->auth->mobile;
      $name=$this->request->request('name');
      $homediv=$this->request->request('homediv');
      $code=rand(111111, 999999);
      $lon=$this->request->request('lon');
      $lat=$this->request->request('lat');
      if($postion!=3){
        $this->error('当前身份不是商家');
      }
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      if($store){
        $this->error('您已经拥有一个店铺');
      }
      $data=[
        'uid'=>$uid,
        'phone'=>$mobile,
        'name'=>$name,
        'store_code'=>$code,
        'lon'=>$lon,
        'lat'=>$lat,
        'homediv'=>$homediv,
          'createtime'=>time(),
      ];
      $res=\app\common\model\Store::create($data);
      if($res){
        $this->success('成功');
      }else{
        $this->error('失败');
      }

    }


    /**
     * 查看当前店铺邀请码
     */
    public function showStore(){
      $uid=$this->auth->id;
      $store_code=\app\common\model\Store::where(['uid'=>$uid])->field('store_code')->find();
      $this->success('成功',$store_code);
    }


  /**
   * 根据城市筛选技师
   */
  public function cityTechnician(){
     $city=$this->request->request('city');//服务城市
     $type=$this->request->request('type');//类型 1 本店技师 2 平台技师
    $time=time();
    $times=datetime($time,'Y-m-d');
     $uid=$this->auth->id;
     $week=date('w',strtotime($times));
     $postion=$this->auth->postion;
     if(!$city){
       $this->error('参数不能为空');
     }
     if(!strpos($city,'市')){
        $city=$city.'市';
     }
     if($postion!=3){
       $this->error('当前身份不是商家');
     }
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     if($type==1){
       $technician=\app\common\model\Technician::where(['store_id'=>$store['store_id'],'service_area'=>['like',"%$city%"]])->field('technician_address,uid,technician_id,technician_avatar,real_name,order_count')->select();
     }else if($type==2){
       $technician=\app\common\model\Technician::where(['service_area'=>['like',"%$city%"]])->field('technician_address,uid,technician_id,technician_avatar,real_name,order_count')->select();
     }
     foreach ($technician as $key=>$value){
       $tech=Technician::where(['technician_id'=>$value['technician_id'],'work_day'=>['like',"%$week%"]])->find();
       if(!$tech){
         $technician_status=2;
       }else{
         $technician_status=1;
       }
       Technician::where(['technician_id'=>$value['technician_id']])->update(['technician_status'=>$technician_status]);
       $use=\app\admin\model\User::where(['id'=>$value['uid']])->find();
       if($use){
         $value['prevtime']=date('Y-m-d H:i',$use->prevtime);
       }
       $value['technician_status']=$technician_status;
       $busy_hour=Db::name('busy_hour')->where(['technician_id'=>$value['technician_id'],'detail'=>['like',"%$times%"]])->find();
       $hour=json_decode($busy_hour['detail']);
       $value['hour']=$hour??[];
       if(empty($hour)){
            $hour=Quantum::all();
            $value['hour']=$hour;
            foreach ($hour as $item){
                $item['status']=2;
            }
       }
     }
     $this->success('查询成功',$technician??[]);
  }

  /**
   * 商家添加服务
   */

  public function addserve(){
    $uid=$this->auth->id;
    $postion=$this->auth->postion;
    $serve_name=$this->request->request('serve_name');//服务名称
    $serve_price=$this->request->request('serve_price');//服务名称
    $serve_image=$this->request->request('serve_image');//服务图片
    $serve_cid=$this->request->request('serve_cid');//服务分类
    $serve_duration=$this->request->request('serve_duration');//服务时长
    $serve_details=$this->request->request('serve_details');//服务描述
    $purchase_count=$this->request->request('purchase_count');//起购数量
      $time_label=$this->request->request('time_label');//时间标签
    $graphic_details=$this->request->request('graphic_details','','trim');//图文详情
    $is_added=$this->request->request('is_added',0);//0 上架 1 下架 默认上架
//      $graphic_details=$graphic_details?html_entity_decode($graphic_details):'';
//      if(!empty($details)){
//          $graphic_details = trim($graphic_details);
//      }
    if($postion!=3){
      $this->error('当前身份不是商家');
    }
    $store=\app\common\model\Store::where(['uid'=>$uid])->find();
    $data=[
      'serve_name'=>$serve_name,
      'serve_image'=>$serve_image,
      'serve_duration'=>$serve_duration,
      'serve_price'=>$serve_price,
      'serve_details'=>$serve_details,
      'serve_status'=>3,
      'store_id'=>$store->store_id,
      'createtime'=>time(),
      'serve_cid'=>$serve_cid,
      'purchase_count'=>$purchase_count,
      'graphic_details'=>$graphic_details,
      'is_added'=>$is_added,
        'time_label'=>$time_label,
      'serve_type'=>0
    ];
    $validate = Loader::validate('Serve');
    if (!$validate->check($data)) {
      $this->error($validate->getError());
    }
    $res=\app\common\model\Serve::create($data);
    TechnicianServe::create([
      'serve_id'=>$res->id,
      'store_id'=>$store->store_id,
       'serve_cid'=>$serve_cid,
      'createtime'=>time(),
      'status'=>1,
      'type'=>0
    ]);
     if($res){
       $this->success('添加成功,待审核');
     }else{
       $this->error('添加失败');
     }
  }


  /**
   * 服务详情
   */

    public function storeServeDetail(){
      $serve_id=$this->request->request('serve_id');
      if(!$serve_id){
        $this->error('参数不能为空');
      }
      $serve=\app\common\model\Serve::where(['id'=>$serve_id])->field('serve_name,id,serve_image,serve_duration,serve_price,serve_details,graphic_details,is_added,purchase_count,serve_cid,serve_type,time_label,label')->find();
      $servec=Servec::where(['id'=>$serve->serve_cid])->find();
      $label=ServeLabel::where(['id'=>$serve->label])->find();
      if($label) {
          $serve['label_name'] = $label->name;
      }
      $serve['cname']=$servec->cname;
      $this->success('查询成功',$serve);
    }


    /**
     * 店铺服务删除
     */

    public function delServe(){
      $uid=$this->auth->id;
      $postion=$this->auth->postion;
      if($postion!=3){
        $this->error('当前身份不是商家');
      }
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      $serve_id=$this->request->request('serve_id');
      $type=$this->request->request('type');//1 自建项目,2 直选项目
      if(!$serve_id){
        $this->error('参数不能为空');
      }
      if($type==1){
        $serve=\app\common\model\Serve::where(['id'=>$serve_id,'serve_type'=>0])->find();
        $te=TechnicianServe::where(['serve_id'=>['in',$serve_id],'store_id'=>['in',$store->store_id]])->find();
        $serve->delete();
        $te->delete();
      }else if($type==2){
       $serve=\app\common\model\Serve::where(['id'=>$serve_id,'serve_type'=>1])->find();
        TechnicianServe::where(['serve_id'=>['in',$serve_id]])->update(['store_id'=>0]);
        $serve->delete();
      }
      $this->success('操作成功');
    }


    /**
     * 店铺审核记录
     */

    public function storeAudit(){
      $uid=$this->auth->id;
      $list=StoreExamine::where(['uid'=>$uid])->field('id,name,createtime,status,type,serve_cid')->select();
      foreach ($list as $item){
        $item['createtime']=date('Y-m-d',$item['createtime']);
          $servec=Servec::where(['id'=>$item['serve_cid']])->find();
          $item['cname']=$servec['cname'];
      }
      $this->success('',$list);
    }

    /**
     * 店铺审核详情
     */

    public function storeAuditDetail(){
      $id=$this->request->request('id');
     $list=StoreExamine::where(['id'=>$id])->field('id,name,status,type,serve_cid,reject_reason')->find();
     $servec=Servec::where(['id'=>$list['serve_cid']])->find();
     $list['cname']=$servec['cname'];
      $this->success('',$list);
    }


    /**
     * 店铺认证
     */

    public function storeCheck(){
      $uid=$this->auth->id;
      $postion=$this->auth->postion;
      $company_name=$this->request->request('company_name');
      $type=$this->request->request('type');//认证类型 1 企业认证 2 个体工商户认证
      $name=$this->request->request('name');//法人姓名
      $license=$this->request->request('license');//营业执照
      $identity_positive=$this->request->request('identity_positive');//身份证正面
      $identity_reverse=$this->request->request('identity_reverse');//身份证反面
      $identity=$this->request->request('identity');//身份证号
      $credit_code=$this->request->request('credit_code');//社会信用代码
      if($postion!=3){
        $this->error('当前身份不是商家');
      }
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      if($store){
        if($store->is_check==1){
          $this->error('您已实名认证');
        }
      }
      $check=EnterPriseCheck::where(['store_id'=>$store->store_id])->find();
      if($check&& $check->status==3){
        $this->error('实名认证信息正在审核中');
      }
      $data=[
        'credit_code'=>$credit_code,
        'company_name'=>$company_name,
        'identity_positive'=>$identity_positive,
        'identity_reverse'=>$identity_reverse,
        'name'=>$name,
        'identity'=>$identity,
        'license'=>$license,
        'type'=>$type,
        'status'=>3,
        'store_id'=>$store->store_id,
        'createtime'=>time()
      ];
      $validate = Loader::validate('EnterPriseCheck');
      if (!$validate->check($data)) {
        $this->error($validate->getError());
      }
     $res=EnterPriseCheck::create($data);
     if($res){
       $this->success('认证成功,待审核');
     }else{
       $this->error('认证失败');
     }
    }


    /**
     * 店铺设置
     */


    public function SetStore(){
      $uid=$this->auth->id;
      $store_recruit=$this->request->request('store_recruit');//店招图片
      $image=$this->request->request('image');//店铺封面
      $hours_start=$this->request->request('hours_start');//店铺营业时间开始
      $hours_end=$this->request->request('hours_end');//店铺营业时间结束
      $serve_phone=$this->request->request('serve_phone');//客服电话
      $serve_image=$this->request->request('serve_image');//客服图片
      $store_welcomes=$this->request->request('store_welcomes');//店铺欢迎语
      $store_introduction=$this->request->request('store_introduction');//店铺简介
      $work_day=$this->request->request('work_day','');
      $name=$this->request->request('name','');
      $homediv=$this->request->request('homediv','');
      $lon=$this->request->request('lon',0);
      $lat=$this->request->request('lat',0);
      $work_day=$work_day?html_entity_decode($work_day):'';
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      if($store_recruit){
        $store->store_recruit=$store_recruit;
      }
      if($image){
        $store->image=$image;
      }
      if($name){
        $store->name=$name;
      }
      if($hours_start){
        $store->hours_start=$hours_start;
      }
      if($hours_end){
        $store->hours_end=$hours_end;
      }
      if($serve_phone){
        $store->serve_phone=$serve_phone;
      }
      if($serve_image){
        $store->serve_image=$serve_image;
      }
      if($store_welcomes){
        $store->store_welcomes=$store_welcomes;
      }
      if($lon){
        $store->lon=$lon;
      }
      if($lat){
        $store->lat=$lat;
      }
      if($store_introduction){
        $store->store_introduction=$store_introduction;
      }
      if($work_day){
        $store->work_day=$work_day;
      }
      if($homediv){
        $store->homediv=$homediv;
      }
      $store->save();
      $this->success('成功');
    }

    /**
     * 服务审核记录
     */

    public function ServeAuit(){
      $uid=$this->auth->id;
      $type=$this->request->request('type');//类型 0 自建项目、1 直选项目
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      $page=$this->request->request('page',1);
      $tserve=TechnicianServe::where(['store_id'=>$store->store_id,'type'=>$type])->group('serve_id')->column('serve_id');

      if($tserve){
            $serve=\app\common\model\Serve::where(['is_added'=>0,'id'=>['in',$tserve]])->field('id,serve_name,serve_price,serve_image,serve_status,sales,serve_cid,serve_type')->page($page,10)->select();
            foreach ($serve as $item){
               $servec=Servec::where(['id'=>$item['serve_cid']])->find();
               $item['cname']=$servec->cname;
            }
            $this->success('',$serve);
      }
      $this->success('',$serve??[]);
    }


    /**
     * 直选列表
     */
    public function DirectlyList(){
      $cid=$this->request->request('cid');
      if(!$cid){
        $this->error('服务分类不能为空');
      }
      $uid=$this->auth->id;
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      $serve=\app\common\model\Serve::where(['is_added'=>0,'serve_type'=>1,'serve_cid'=>$cid])->field('id,serve_name,serve_price,serve_image,serve_status')->select();
      foreach ($serve as $item){
        $store_serve=TechnicianServe::where(['serve_id'=>$item['id'],'store_id'=>$store['store_id'],'status'=>1])->select();
        if($store_serve){
          $is_add='已加入';
        }else{
          $is_add='未加入';
        }
        $item['is_add']=$is_add;
      }
      $this->success('',$serve);
    }


    /**
     * 加入直选
     */

    public function addDirect(){
      $cid=$this->request->request('cid');//服务分类id
      $serve_id=$this->request->request('serve_id');//服务项目id
      $uid=$this->auth->id;
      $store=\app\common\model\Store::where(['uid'=>$uid])->find();
      $tserve=TechnicianServe::where(['store_id'=>$store->store_id,'serve_id'=>$serve_id])->find();
      if($tserve&&$tserve->status==3){
          $this->error('该项目还在审核中');
      }
      $data=[
        'serve_id'=>$serve_id,
        'serve_cid'=>$cid,
        'status'=>3,
        'type'=>1,
        'createtime'=>time(),
        'store_id'=>$store->store_id,
      ];
      $res=TechnicianServe::create($data);
      if($res){
        $this->success('添加成功,待审核');
      }else{
        $this->error('添加失败');
      }
    }


    /**
     * 店铺主页
     */

    public function storehome(){
       $uid=$this->auth->id;
       $store=\app\common\model\Store::where(['uid'=>$uid])->find();
       $order=\app\admin\model\Order::where(['store_id'=>$store['store_id'],'order_status'=>['in',[1,2,3,4,5]]])->whereTime('pay_time','today')->count()??0;
      $pay_money=\app\admin\model\Order::where(['store_id'=>$store['store_id'],'order_status'=>['in',[1,2,3,4,5]]])->whereTime('pay_time','today')->sum('order_amount');
      $amount=\app\admin\model\Order::where(['store_id'=>$store['store_id'],'order_status'=>['in',[1,2,3,4,5]]])->whereTime('pay_time','today')->sum('plusitem_money');
      $paymoney=bcadd($pay_money,$amount,2);
      $today_order=\app\admin\model\Order::where(['store_id'=>$store['store_id'],'order_status'=>['in',[6,8]]])->whereTime('order_time','today')->count()??0;
      $info_money=\app\admin\model\Order::where(['store_id'=>$store['store_id'],'order_status'=>['in',[6,8]]])->whereTime('order_time','today')->sum('order_amount');
      $platitem_money=\app\admin\model\Order::where(['store_id'=>$store['store_id'],'order_status'=>['in',[6,8]]])->whereTime('order_time','today')->sum('plusitem_money');
      $today_money=bcadd($info_money,$platitem_money,2);
       $data=[];
       $storee=EnterPriseCheck::where(['store_id'=>$store['store_id']])->find();
       $data['status']=$storee['status'];//店铺认证审核状态 //1 审核通过 2 审核失败 3 审核中
       $follow=Follow::where(['follow_status'=>1,'store_id'=>$store['store_id']])->count();
       $data['follow']=$follow;//被关注数量
       $data['image']=$store['image'];//店铺封面
       $data['start']=$store['hours_start'];//店铺营业时间开始
      $data['end']=$store['hours_end'];//店铺营业时间结束
      $data['name']=$store['company_name'];//认证公司名称
      $data['store_name']=$store['name'];//店铺名称
      $data['pay_count']=$order;//今日支付单数
      $data['pay_money']=$paymoney;//今日支付金额
      $data['done_money']=$today_money; //今日成交金额
      $data['done_count']= $today_order;//今日成交单数
      $this->success('查询成功',$data);

    }


    /**
     * 商家端技师详情
     */
           public function Techniciandetail(){
             $technician_id=$this->request->request('technician_id');
             $technician=\app\common\model\Technician::where(['technician_id'=>$technician_id])->field('uid,technician_id,is_check,real_name,technician_phone,introduction,technician_status,serve_time,serve_time_end,service_area,technician_address,comprehensive_scoring,store_id,technician_avatar')->find();
             $checkuser=CheckUser::where(['uid'=>$technician['uid']])->find();
             $te=TechnicianServe::where(['technician_id'=>$technician_id,'store_id'=>$technician['store_id'],'status'=>1])->count();
             $technician['count']=$te;
             $technician['status']=$checkuser['status']??0;
             if(!$technician['serve_time']||!$technician['serve_time_end']){
               $technician['serve_time']='暂未设置';
                 $technician['serve_time_end']='暂未设置';
             }
             $this->success('',$technician);
           }
           /**
            * 技师下面的服务项目
            */
   public function technician_serve(){
$uid=$this->auth->id;
$technician_id=$this->request->request('technician_id');
$postion=$this->auth->postion;
if($postion!=3){
  $this->error('当前身份不是商家');
   }

 if(empty($technician_id)){
     $this->error('请先选择服务时间');
 }

  $store=\app\admin\model\Store::where(['uid'=>$uid])->find();


   $tech=\app\common\model\Technician::where(['technician_id'=>$technician_id,'store_id'=>$store->store_id])->find();

   if($tech) {
     $tserve = TechnicianServe::where(['technician_id' => ['in',$technician_id],'status'=>1,'store_id'=>$store->store_id])->field('serve_id,serve_cid')->select();
     foreach ($tserve as $item) {
       $item['serve'] = \app\common\model\Serve::where(['id' => $item['serve_id'], 'serve_status' => 1, 'is_added' => 0])->field('id,serve_name,serve_image,serve_duration,serve_cid,serve_price')->find();
       $servec = Servec::where(['id' => $item['serve_cid']])->find();
       $item['cname'] = $servec->cname;
     }
   }else{
       $this->error('该技师没有加入该商家');
   }
   $this->success('',$tserve??[]);
   }


   /**
    * 商家下面的服务详情里面的技师列表
    *
    */
   public function ServeTechnician(){
     $uid=$this->auth->id;
     $time=time();
     $times=datetime($time,'Y-m-d');
     $week=date('w',strtotime($times));
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     $serve_id=$this->request->request('serve_id');
     $technician_ids=TechnicianServe::where(['serve_id'=>['in',$serve_id],'status'=>1,'store_id'=>$store->store_id])->group('technician_id')->column('technician_id');
     $technician=Technician::where(['technician_id'=>['in',$technician_ids]])->field('technician_id,technician_name,technician_avatar,uid,real_name,order_count,technician_address')->select();
     foreach ($technician as $key=>$value) {
       $tech=Technician::where(['technician_id'=>$value['technician_id'],'work_day'=>['like',"%$week%"]])->find();
       if(!$tech){
         $technician_status=2;
       }else{
         $technician_status=1;
       }
       Technician::where(['technician_id'=>$value['technician_id']])->update(['technician_status'=>$technician_status]);
       $user=\app\admin\model\User::where(['id'=>$value['uid']])->find();
       if($user){
         $value['prevtime'] = date('Y-m-d H:i',$user->prevtime);
       }
       $value['technician_status']=$technician_status;
       $busy_hour=Db::name('busy_hour')->where(['technician_id'=>$value['technician_id'],'detail'=>['like',"%$times%"]])->find();
       $hour=json_decode($busy_hour['detail']);
       $value['hour']=$hour??[];
     }
    $this->success('',$technician);
   }


   /**
    * 商家下面的技师列表
    */
   public function store_technicianlist(){
     $uid=$this->auth->id;
     $city=$this->request->request('city');//城市搜索
       if(!strpos($city,'市')){
           $city=$city.'市';
       }
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     $technician=Technician::where(['store_id'=>$store->store_id,'service_area'=>['like',"%$city%"]])->field('real_name,technician_id,technician_name,technician_avatar,technician_status,technician_address,lon,lat,technician_phone')->select();
     $this->success('', $technician);
   }

   /**
    * 商家端评论管理
    */
   public function store_comment(){
     $uid=$this->auth->id;
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     $type=$this->request->request('type');//1 好评 2 中评 3 差评
     if($type){
       $where['type']=$type;
     }
     $page=$this->request->request('page');
     $where['store_id']=$store->store_id;
     $comment=OrderComment::where($where)->page($page,10)->field('id,uid,address,technician_id,star,reply_content,serve_id,createtime,image,content')->select();
     $haoping=OrderComment::where(['star'=>5,'store_id'=>$store->store_id])->count();
     $zhongping=OrderComment::where(['star'=>['in',[2,3,4]],'store_id'=>$store->store_id])->count();
     $chaping=OrderComment::where(['star'=>1,'store_id'=>$store->store_id])->count();
     $quanbu=OrderComment::where(['store_id'=>$store->store_id])->count();
     $youtu=OrderComment::where(['store_id'=>$store->store_id,'type'=>4])->count();
     foreach ($comment as $item){
       $item['image']=explode(',',$item['image']);
       $item['haoping']=$haoping;
       $item['zhongping']=$zhongping;
       $item['chaping']=$chaping;
       $item['quanbu']=$quanbu;
       $item['youtu']=$youtu;
       $item['createtime']=date('Y-m-d H:i',$item['createtime']);
       $item['user']=\app\common\model\User::where(['id'=>$item['uid'],'del_flag'=>0])->field('id,avatar,nickname')->find();
       $item['technician']= \app\common\model\Technician::where('technician_id', '=', $item['technician_id'])->field('technician_id,store_id,technician_avatar,real_name')->find();
      $item['serve'] = \app\common\model\Serve::where('id', '=', $item['serve_id'])->field('id,serve_name')->find();
     }
     $this->success('',$comment);
   }


   /**
    * 评论回复
    */

   public function commentReply(){

     $uid=$this->auth->id;
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     $comment_id=$this->request->request('comment_id');
     $content=$this->request->request('content');
     $coment=OrderComment::where(['id'=>$comment_id])->find();
     if($coment->store_id!=$store->store_id){
       $this->error('当前商家不一致');
     }
     if(!$content){
       $this->error('回复内容不能为空');
     }
     $coment->reply_content=$content;
     $coment->reply_store_id=$store->store_id;
     $coment->save();
    $this->success('成功');
   }

   /**
    * 店铺的投诉管理
    */

   public function store_inform(){
     $uid=$this->auth->id;
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     $inform=InformLog::where(['store_id'=>$store->store_id])->field('id,uid,order_id,content,inform_title,inform_time,is_sendto,sendto')->select();
     foreach ($inform as $key=>$value){
       $value['user']=\app\common\model\User::where(['id'=>$value['uid'],'postion'=>1,'del_flag'=>0])->field('id,nickname,avatar')->find();
       $value['order']=\app\common\model\Order::where(['id'=>$value['order_id']])->field('id,order_sn')->find();
       $value['info_time']=date('Y-m-d',$value['inform_time']);
       $value['info_title']=explode(',',$value['inform_title']);
     }
     $this->success('查询成功',$inform);
   }


   /**
    * 店铺技师解约
    */

   public function technicianTermination(){
     $time=time();
     $uid=$this->auth->id;
     $technician_id=$this->request->request('technician_id');
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     $technician=Technician::where(['technician_id'=>$technician_id,'store_id'=>$store->store_id])->find();
     if($technician['store_id']==0){
       $this->error('当前技师已解约');
     }
     $mobile          = \app\admin\model\User::where('id', $technician['uid'])->value('mobile');
     if($technician['technician_phone'] ){
       $mobile = $technician['technician_phone'];
     }
     if($mobile){
       $res = \app\common\model\Sms::termination_sms($technician['technician_name'],$mobile,date('Y-m-d H:i:s',$time));
       adlog('阿里短信', json_encode($res));
       if($res->body->code == 'OK'){
         $sms  = \app\common\model\Sms::create(['event' => 'termination','mobile' => $mobile,'code' => $technician_id,'ip' => '','createtime' => $time]);
       }else{
         adlog('解约技师短信发送失败', json_encode($res));
       }
       $res=Technician::where(['technician_id'=>$technician_id])->update(['store_id'=>0,'is_join_store'=>0]);
       $this->success('解约成功');
     }
   }


   /**
    * 商家技师详情下面的服务详情
    */

   public function technicianServeDetail(){
     $serve_id=$this->request->request('serve_id');
     $uid=$this->auth->id;
     if(!$serve_id){
       $this->error('参数不能为空');
     }
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     if($store) {
         $serve = \app\common\model\Serve::where(['id' => $serve_id, 'is_added' => 0, 'serve_status' => 1])->field('serve_name,serve_image,serve_duration,serve_price,serve_details')->find();
         $tserve = TechnicianServe::where(['serve_id' => $serve_id, 'status' => 1,'store_id'=>$store['store_id']])->group('technician_id')->column('technician_id');
         $technician = Technician::where(['technician_id' => ['in', $tserve], 'store_id' => $store->store_id])->field('technician_id,technician_avatar')->select();
         if ($technician) {
             foreach ($technician as $item) {
                 $isset_order = \app\common\model\Order::where(['technician_id' => $item['technician_id'], 'order_status' => ['IN', [2, 3, 4, 5]]])->find();
                 if ($isset_order) {
                     $serve['earliestappionent_time'] = date('H:i', $isset_order['endserve_time']);
                 } else {
                     $serve['earliestappionent_time'] = date('H:i', time());
                 }
             }
         }
         $serve['technician'] = $technician;
         $this->success('', $serve);
     }
   }


   /**
    * 商家技师搜索
    */

   public function search_technicianlist(){
     $uid=$this->auth->id;
     $address=$this->request->request('address');//地址搜索
     $type=$this->request->request('type');//1 商家技师 2 平台技师
     $time=time();
     $times=datetime($time,'Y-m-d');
     $week=date('w',strtotime($times));
     $postion=$this->auth->postion;
     if(!$address){
       $this->error('参数不能为空');
     }
     if($postion!=3){
       $this->error('当前身份不是商家');
     }
     $store=\app\common\model\Store::where(['uid'=>$uid])->find();
     if($type==1){
       $technician=\app\common\model\Technician::where(['store_id'=>$store->store_id,'service_area'=>['like',"%$address%"]])->field('technician_address,technician_id,uid,technician_avatar,real_name,order_count')->select();
     }else if($type==2){
       $technician=\app\common\model\Technician::where(['store_id'=>['neq',$store->store_id],'service_area'=>['like',"%$address%"]])->field('technician_address,uid,technician_id,technician_avatar,real_name,order_count')->select();
     }
     foreach ($technician as $key=>$value){
       $user=\app\admin\model\User::where(['id'=>$value['uid'],'postion'=>2])->find();
       if($user){
         $value['prevtime']=date('Y-m-d H:i',$user->prevtime);
       }
       $tech=Technician::where(['technician_id'=>$value['technician_id'],'work_day'=>['like',"%$week%"]])->find();
       if(!$tech){
         $technician_status=2;
       }else{
         $technician_status=1;
       }
       Technician::where(['technician_id'=>$value['technician_id']])->update(['technician_status'=>$technician_status]);
       $value['technician_status']=$technician_status;
       $busy_hour=Db::name('busy_hour')->where(['technician_id'=>$value['technician_id'],'detail'=>['like',"%$times%"]])->find();
       $hour=json_decode($busy_hour['detail']);
       $value['hour']=$hour??[];

     }
     $this->success('查询成功',$technician??[]);
   }


   /**
    * 更新介绍
    * type 1 用户端 2 技师端 3 商家端
    */

   public function updateIntroList(){
     $type=$this->request->request('type');
     $where=[];
     if($type){
       $where['type']=$type;
     }
     $list=Updateintro::where($where)->select();
     $this->success('查询成功',$list);
   }

   /**
    * 更新介绍详情
    */
   public function updateIntrodetail(){
     $id=$this->request->request('id');
     if(!$id){
       $this->error('参数不能为空');
     }
     $where=[];
     if($id){
       $where['id']=$id;
     }
     $list=Updateintro::where($where)->find();
     $this->success('查询成功',$list);
   }


   /**
    * 查看店铺设置
    */

   public function showsetstore(){
     $uid=$this->auth->id;
     $store=\app\admin\model\Store::where(['uid'=>$uid])->field('store_id,name,image,homediv,store_address,hours_start,hours_end,serve_phone,work_day,serve_image,store_introduction,store_welcomes,store_recruit,store_grade,is_check')->find();
     if($store){
       $check=EnterPriseCheck::where(['store_id'=>$store->store_id])->find();
       if($check){
           $audit_status=$check->status;
           $store['audit_status']=$audit_status;
       }
         $status=1;//当前登录用户是否有店铺
         $store['status']=$status;
       $store['work_day']=explode(',',$store['work_day']);
     }else{
       $store=[];
       $status=0;//当前登录用户无店铺
       $store['status']=$status;
     }
     $this->success('查询成功',$store);
   }

   /**
    * 查看当前店铺的实名状态
    */

   public function showAudit(){
     $uid=$this->auth->id;
     $store=\app\admin\model\Store::where(['uid'=>$uid])->find();
     $check=EnterPriseCheck::where(['store_id'=>$store->store_id])->find();
     $this->success('查询成功',$check);
   }

   /**
    * 服务分类
    */

   public function servec(){
       $servec=Servec::all();
       $this->success('',$servec);
   }

   /**
    * 修改自建服务
    */

   public function updateServe(){
       $serve_id=$this->request->request('serve_id');
       $serve_name=$this->request->request('serve_name');//服务名称
       $serve_price=$this->request->request('serve_price');//服务名称
       $serve_image=$this->request->request('serve_image');//服务图片
       $serve_cid=$this->request->request('serve_cid');//服务分类
       $serve_duration=$this->request->request('serve_duration');//服务时长
       $serve_details=$this->request->request('serve_details');//服务描述
       $purchase_count=$this->request->request('purchase_count');//起购数量
//       $graphic_details=$this->request->request('graphic_details');//图文详情
//       $graphic_details=$graphic_details?html_entity_decode($graphic_details):'';
       $graphic_details=$this->request->request('graphic_details','','trim');
       adlog('参数', $graphic_details);
//       if(!empty($details)){
//           $graphic_details = trim($graphic_details);
//       }
       $is_added=$this->request->request('is_added',0);//0 上架 1 下架 默认上架
       $label=$this->request->request('label',0);//0 服务标签
       $time_label=$this->request->request('time_label',0);//0 服务标签
       if(!$serve_id){
           $this->error('当前参数不能为空');
       }
       $serve=\app\admin\model\Serve::where(['id'=>$serve_id,'serve_type'=>0])->find();
       if(!$serve){
           $this->error('当前服务不存在');
       }
       if($serve_name){
          $serve->serve_name=$serve_name;
       }
       if($serve_price){
           $serve->serve_price=$serve_price;
       }
       if($serve_image){
           $serve->serve_image=$serve_image;
       }
       if($serve_cid){
           $serve->serve_cid=$serve_cid;
       }
       if($serve_duration){
           $serve->serve_duration=$serve_duration;
       }
       if($serve_details){
           $serve->serve_details=$serve_details;
       }
       if($purchase_count){
           $serve->purchase_count=$purchase_count;
       }
       if($graphic_details){
           $serve->graphic_details=$graphic_details;
       }
       if($is_added){
           $serve->is_added=$is_added;
       }
       if($label){
           $serve->label=$label;
       }
       if($time_label){
           $serve->time_label=$time_label;
       }
       $serve->save();
       $this->success('修改成功');
   }

   /**
    * 筛选店铺服务
    */


   public function searchserve(){
       $uid=$this->auth->id;
       $store=\app\admin\model\Store::where(['uid'=>$uid])->find();
       $tserve=TechnicianServe::where(['store_id'=>$store['store_id'],'status'=>1])->group('serve_id')->column('serve_id');
       if($tserve){
           $serve=\app\admin\model\Serve::where(['id'=>['in',$tserve],'serve_status'=>1])->field('id,serve_name,serve_price')->select();
           $this->success('查询成功',$serve);
       }
       $this->success('查询成功',[]);
   }
}